<?php

namespace common\widgets\grid;

/**
 * Class FooterCellColumn
 *
 * @package common\widgets\grid
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class FooterCellColumn extends \yii\grid\DataColumn {

    /**
     * @var mixed|null
     */
    private $_footerContent;

    /**
     * 获取当前表格底部单元格值
     *
     * @return mixed
     */
    protected function getFooterContent() {
        return $this->_footerContent;
    }

    /**
     * 设置表格底部单元格值
     *
     * @param $content
     */
    protected function setFooterContent($content) {
        $this->_footerContent = $content;;
    }

    /**
     * 自定义表格底部数值内容
     *
     * @param mixed $value
     */
    abstract protected function handleFooterCellValue($value);

    /**
     * {@inheritdoc}
     */
    protected function renderDataCellContent($model, $key, $index) {
        if ($this->content === null) {
            $value = $this->getDataCellValue($model, $key, $index);
            $this->handleFooterCellValue($value);
            return $this->grid->formatter->format($value, $this->format);
        }
        return call_user_func($this->content, $model, $key, $index, $this);
    }

    /**
     * {@inheritdoc}
     */
    protected function renderFooterCellContent() {
        $content = $this->getFooterContent();
        if ($content) {
            return $this->grid->formatter->format($content, $this->format);;
        }
        return $this->grid->emptyCell;
    }

}